/**
 * Created with IntelliJ IDEA.
 * User: Lorenzo
 * Date: 14-12-31
 * Time: ����3:26
 * To change this template use File | Settings | File Templates.
 */
// Is a given variable an object?
isObject = function (obj) {
    return obj === Object(obj);
};
/**
 * @description read template file and compile it.
 * <p>Base on handlebars official website solution it will write a lot of template block in html file,
 * <p>so we use a trick solution that with the help of AJAX, we can divide template code to file, and
 * <p>if we want use precompile, it don't need modify anything and only need to run precomplied cmd.
 * <p>Attention : files directory default name templates, so pls save file in templates folder.
 * @see http://berzniz.com/post/24743062344/handling-handlebars-js-like-a-pro
 * @param {String} name template file name.
 * @param path
 */
Handlebars.getTemplate = function (name, path) {
    path = path || "template/";
    if (Handlebars.templates === undefined || Handlebars.templates[ name ] === undefined) {
        $.ajax({
            cache: false,
            url: path + name + '.template',
            dataType: 'html',
            contentType: "application/json; charset=GBK",
            success: function (data /*, textStatus , jqXHR*/) {
                if (Handlebars.templates === undefined) {
                    Handlebars.templates = {};
                }
                Handlebars.templates[ name ] = data;
            },
            async: false
        });
    }
    return Handlebars.templates[ name ];
};

Handlebars.complieTemplate = function () {
    var compiledTemplate = Handlebars.compile(arguments[0]);
    var ulrOrData = arguments[1] || false;
    var dataType = arguments[2] || false;
    var compiledHTMLCode = "";
    if (((ulrOrData && isObject(ulrOrData)) && !dataType)) {
        compiledHTMLCode = compiledTemplate(ulrOrData);
    }
    else if (ulrOrData && dataType) {
        $.ajax({
            url: ulrOrData,
            dataType: dataType,
            cache: false,
            success: function (data /*, textStatus , jqXHR*/) {
                compiledHTMLCode = compiledTemplate(data);
            },
            error: function (jqXHR, Reason, description) {
                console.error(Reason + ":" + description);
            },
            async: false
        });
    }
    else {
        compiledHTMLCode = compiledTemplate();
    }
    return compiledHTMLCode;
};
//ע��һ���Ƚ��Ƿ���ȵ�help
Handlebars.registerHelper("compare", function(lvalue, rvalue, options) {
    if (arguments.length < 3) {
        throw new Error("Handlerbars Helper 'compare' needs 2 parameters");
    }
    var operator = options.hash.operator || "==";
    var operators = {
        '==' : function(l, r) {
            return l === r;
        },
        '!=' : function(l, r) {
            return l !== r;
        },
        '<' : function(l, r) {
            return l < r;
        },
        '>' : function(l, r) {
            return l > r;
        },
        '<=' : function(l, r) {
            return l <= r;
        },
        '>=' : function(l, r) {
            return l >= r;
        }
    };
    if (!operators[operator]) {
        throw new Error("Handlerbars Helper 'compare' doesn't know the operator " + operator);
    }
    var result = operators[operator](lvalue, rvalue);
    if (result) {
        return options.fn(this);
    } else {
        return options.inverse(this);
    }
});


/**
 * 重写post请求，同步操作
 * @param url
 * @param postData
 * @returns {*}
 */
function syncPostRequest(url, postData) {
    var returnData;
    $.ajax({
        url: url,
        type: 'POST',
        cache: false,
        async: false,
        contentType: 'application/json',
        dataType: "json",
        data:JSON.stringify(postData),
        success: function(data) {
            returnData=data;
        },
        error: function(data){
            returnData = {
                status: 0,
                data: "服务器内部异常"
            }
        }
    })
    return returnData;
}

/**
 * 重写Get请求，同步操作
 * @param url
 * @returns {*}
 */
function syncGetRequest(url) {
    var returnData;
    $.ajax({
        url: url,
        type: 'GET',
        cache: false,
        async: false,
        contentType: 'application/json',
        dataType: "json",
        success: function(data) {
            returnData=data;
        },
        error: function(data){
            returnData = {
                status: 0,
                data: "服务器内部异常"
            }
        }
    })
    return returnData;
}